<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改页</title>
    <link rel="stylesheet" href="./static/css/index.css">
</head>

<body>

    <div>
        <div>
            <h1>修改</h1>
        </div>
        <div>
            <table>
                <tbody>
                    <tr>
                        <td>
                            姓名: <input id="uname" type="text" placeholder="请输入姓名">
                        </td>
                        <td>
                            年龄: <input id="uage" type="text" placeholder="请输入年龄">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="button" value="确认" onclick="saveU()">
                            <input type="button" value="返回" onclick="cancelU()">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>


    <script src="./static/js/axios.min.js"></script>
    <script src="./static/js/jquery-3.7.1.min.js"></script>
    <script>

        const api = "http://localhost:3000"

        const id = new URLSearchParams(location.search).get("id")
        console.log(id);

        (() => {
            axios.get(`${api}/user/${id}`).then((res) => {
                console.log(res.data.data);
                
                let uname = $('#uname').val(res.data.data.uname)
                let uage = $('#uage').val(res.data.data.uage)
            })
        })()

        function saveU() {
            let uname = $('#uname').val()
            let uage = $('#uage').val()


            if (confirm("确认吗???")) {
                axios.put(`${api}/user/${id}`, { uname, uage }).then(() => {
                    location.href = './index.html'
                })
            }
        }

        function cancelU() {
            location.href = './index.html'
        }

    </script>

</body>

</html>